/*
 * Jd_ListesInterventions.java
 *
 * Created on 27 avril 2008, 15:40
 */

package run;

import bo.Intervention;
import bo.ListeInterventions;
import boerrors.AdresseInexistantException;
import boerrors.ClientInexistantException;
import boerrors.CodePostalMinimumMultiplicityException;
import boerrors.TechnicienMinimumMultiplicityException;
import boerrors.ZoneMinimumMultiplicityException;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  olivierm
 */
public class Jd_ListesInterventions extends javax.swing.JDialog {
    
    private DefaultTableModel _modele = new DefaultTableModel();
    private String[] colName = {"Id","Status","Description de la demande","Priorité", "Date demande", "Date intervention", "Nom client", "Numéro client","Prénom client", "Matricule Technicien","Nom technicien", "Prénom technicien", "Rue de l'intervention", "Numéro de l'intervention", "Codepostal de l'intervention", "Commune de l'intervention", "Zone de l'intervention"};
    
    
    /** Creates new form Jd_ListesInterventions */
    public Jd_ListesInterventions(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this._modele.setColumnIdentifiers(colName);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jbtn_ListeInterventions = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Liste des objets en mémoire");

        jPanel1.setBackground(new java.awt.Color(0, 102, 153));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Tahoma", 3, 14));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Liste des interventions");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(231, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);

        jXPanel1.setLayout(new java.awt.BorderLayout());

        jToolBar1.setRollover(true);

        jbtn_ListeInterventions.setText("Rafraichir");
        jbtn_ListeInterventions.setFocusable(false);
        jbtn_ListeInterventions.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbtn_ListeInterventions.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbtn_ListeInterventions.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtn_ListeInterventionsActionPerformed(evt);
            }
        });
        jToolBar1.add(jbtn_ListeInterventions);

        jXPanel1.add(jToolBar1, java.awt.BorderLayout.NORTH);

        jTable1.setModel(_modele);
        jScrollPane1.setViewportView(jTable1);

        jXPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jXPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtn_ListeInterventionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtn_ListeInterventionsActionPerformed
        try {
            Vector<Intervention> listeI = ListeInterventions.getInstance().getListeIntervention();
            this._modele.setRowCount(0);
            DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            String[] listeIntervention = new String[17];
            for (int i = 0; i < listeI.size(); i++) {
                listeIntervention[0] = String.valueOf(listeI.get(i).getIdIntervention());
                listeIntervention[1] = listeI.get(i).getIStatut();
                listeIntervention[2] = listeI.get(i).getDescriptionDemande();
                listeIntervention[3] = listeI.get(i).getPrioriteDemande();
                Date dateDate = listeI.get(i).getDateDemande().getTime();
                String dateFormatee = dateFormat.format(dateDate);
                listeIntervention[4] = dateFormatee;
                if (listeI.get(i).getDateIntervention() != null){
                    Date dateDate2 = listeI.get(i).getDateIntervention().getTime();
                    String dateFormatee2 = dateFormat.format(dateDate2);
                    listeIntervention[5] = dateFormatee2;                    
                }
                listeIntervention[6] = String.valueOf(listeI.get(i).getIClient().getIdClient());
                listeIntervention[7] = listeI.get(i).getIClient().getNom();
                listeIntervention[8] = listeI.get(i).getIClient().getPrenom();
                if (listeI.get(i).getITechnicien() != null){
                    listeIntervention[9] = String.valueOf(listeI.get(i).getITechnicien().getMatricule());
                    listeIntervention[10] = listeI.get(i).getITechnicien().getNom();
                    listeIntervention[11] = listeI.get(i).getITechnicien().getPrenom();                    
                }

                listeIntervention[12] = listeI.get(i).getIAdresse().getRue();
                listeIntervention[13] = listeI.get(i).getIAdresse().getNumero();
                listeIntervention[14] = listeI.get(i).getIAdresse().getICodePostal().getCodePostal();
                listeIntervention[15] = listeI.get(i).getIAdresse().getICodePostal().getVille();
                listeIntervention[16] = listeI.get(i).getIAdresse().getICodePostal().getZone().getLibelleZone();
                this._modele.addRow(listeIntervention);
            }
        } catch (ZoneMinimumMultiplicityException ex) {
            Logger.getLogger(Jd_ListesInterventions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CodePostalMinimumMultiplicityException ex) {
            Logger.getLogger(Jd_ListesInterventions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (AdresseInexistantException ex) {
            Logger.getLogger(Jd_ListesInterventions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(Jd_ListesInterventions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TechnicienMinimumMultiplicityException ex) {
            Logger.getLogger(Jd_ListesInterventions.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClientInexistantException ex) {
            Logger.getLogger(Jd_ListesInterventions.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_jbtn_ListeInterventionsActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Jd_ListesInterventions dialog = new Jd_ListesInterventions(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JToolBar jToolBar1;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private javax.swing.JButton jbtn_ListeInterventions;
    // End of variables declaration//GEN-END:variables
    
}
